package cn.com.yusys.yusp.admin.web.rest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.itextpdf.text.PageSize;

import cn.com.yusys.yusp.commons.view.AbstractPdfView;
import cn.com.yusys.yusp.commons.view.PdfView;

@RestController
@RequestMapping("/api/pdfdemo")
public class AdminPdfDemoResource {

    @RequestMapping(value = "/gethtml", method = RequestMethod.GET)
    public ModelAndView gethtml() {
        Map<String, Object> pdfData = buildPdfData();
        ModelAndView modelAndView = new ModelAndView("pdftemps/pdfViewDemo");
        modelAndView.addAllObjects(pdfData);
        return modelAndView;
    }

    @RequestMapping(value = "/getpdf", method = RequestMethod.GET)
    public ModelAndView getpdf(@RequestParam(name = "online", defaultValue = "0") String online) {
        // pdf数据
        Map<String, Object> pdfData = buildPdfData();

        AbstractPdfView pdfView = new PdfView();
        pdfView.setTemplateName("pdftemps/pdfViewDemo"); // 目录路径
        if ("1".equals(online)) {// 是否在线查看
            pdfView.setIsOnlineBrowse(true);
        } else {
            pdfView.setIsOnlineBrowse(false);
        }
        pdfView.setDownloadFileName("pdfdemo.pdf");// 下载后的文件名
        pdfView.setPageSize(PageSize.A4);// pdf纸张大小

        return new ModelAndView(pdfView, pdfData);
    }

    private Map<String, Object> buildPdfData() {
        Map<String, Object> pdfData = new HashMap<>();
        pdfData.put("mode", "pdf");
        pdfData.put("mobile", "13654837902");
        pdfData.put("email", "pdfdemotest@yusys.com.cn");
        pdfData.put("name", "pdfdemo");

        List<Map<String, String>> usrList = new ArrayList<Map<String, String>>();
        for (int i = 0; i < 9; i++) {
            Map<String, String> usr = new HashMap<>();
            usr.put("code", "00" + i);
            usr.put("name", "user00" + i);
            if (i % 2 == 0) {
                usr.put("sex", "Male");
            } else {
                usr.put("sex", "Female");
            }
            usr.put("addr", "Shanghai");
            usrList.add(usr);
        }
        pdfData.put("usrList", usrList);
        return pdfData;
    }
}
